FROM alpine:3.15.0 as base-image

MAINTAINER lilith Bristol
ARG appName
ARG appPackageName=$appName'.jar'

#切换为阿里云源 更新仓库
#安装bash
#安装Java17 安装时区 安装工具箱 安装curl
#更新CA证书
#切换容器时区为东8区时间
#切换应用程序时间为东8区时间
#清除临时文件和安装包
RUN  sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories  \
        && apk update \
        && apk upgrade \
        && apk add --no-cache bash bash-doc bash-completion \
        && apk add openjdk17 busybox tzdata curl \
        && apk add --no-cache ca-certificates \
        && update-ca-certificates \
        && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
        && echo Asia/Shanghai > /etc/timezone \
        && rm -rf /tmp/* /var/cache/apk/* \
        && mkdir /home/app \
        && mkdir /home/app/log

# 启用分层镜像支持
# 中间层用于保存jar包 执行抽取分层
FROM base-image as interl

WORKDIR /home/app
ADD $appPackageName ./
# 使用JDK提供的分层工具打包分层镜像 提高构建速度
RUN java -Djarmode=layertools -jar $appPackageName extract


# 最终层 保存应用包分层内容
FROM base-image

WORKDIR /home/app

COPY --from=interl /home/app/dependencies/ ./
COPY --from=interl /home/app/snapshot-dependencies/ ./
COPY --from=interl /home/app/spring-boot-loader/ ./
COPY --from=interl /home/app/application/ ./

EXPOSE 8998

ENV TZ Asia/Shanghai
ENV dbusername=""
ENV dbpassword=""
ENV dburl=""
ENV JVM_OPTS="-Xmx16G -Xms1G -Xss256k -XX:-ZUncommit -XX:+UseZGC -XX:ConcGCThreads=2 -XX:ZCollectionInterval=120 -XX:ZAllocationSpikeTolerance=5 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+DisableExplicitGC -XX:+PrintClassHistogram -Xlog:safepoint,classhisto*=trace,age*,gc*=info:file=/home/app/logs/gc-%t.log:time,tid,tags:filecount=5,filesize=50m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/app/dump/  -Dcom.sun.management.jmxremote.port=26666 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
ENV JAVA_OPTS="-Dlogging.home=/home/app/log -Dlogging.level.root=INFO"

ENTRYPOINT ["bash","-c","java $JVM_OPTS $JAVA_OPTS -Dspring.profiles.active=prod -Dspring.datasource.hikari.username=$dbusername -Dspring.datasource.hikari.password=$dbpassword -Dspring.datasource.url=$dburl org.springframework.boot.loader.JarLauncher"]
# 运行镜像命令 docker container run -d --name 运行时镜像名 -p 8998:8998 -e dbusername="数据源用户名" -e dbpassword="数据库密码" -e dburl="数据库地址" 镜像名
# docker container run -d --name fast-app -p 8998:8998 -e dbusername="root" -e dbpassword="root" -e dburl="jdbc:mysql://172.27.59.31:3306/fast?autoRec
# onnect=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true"  fast-web-archetype-1.2.1:20211211-1131